//index.js
//获取应用实例
const app = getApp()
var common = require("../../common.js")
Page({

  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    showTip:false,
    qa: [
      { q: '积分有什么用？', a: '消耗积分才可以玩星探游戏' },
      { q: '怎样获取积分？', a: '分享游戏给您的微信群即可获得积分，游戏中选对目标也可以获得积分' },
      { q: '每次分享都可以获得积分吗？', a: '首先必须分享到您的微信群，一个群每天都可获得一次积分' }
    ]
  },

  /**
   * 弹出层函数
   */
  //出现
  showTip: function () {
    this.setData({ showTip: true })
  },
  //消失
  hideTip: function () {
    this.setData({ showTip: false })
  },
  
  loginTimer:null,
  onLoad: function () {    
    const that=this
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {},
      fail: function (res) {}
    })
    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        if (app.globalData.backURL!=''){
          wx.navigateTo({
            url: app.globalData.backURL,
          })
        }
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          if (app.globalData.backURL != '') {
            wx.navigateTo({
              url: app.globalData.backURL,
            })
          }
        }
      })
    }

    that.loginTimer=setInterval(function () {
      if (app.globalData.title == null){
        return;
      }else{
        clearInterval(that.loginTimer);
        that.setData(
          {
            avatarUrl: app.globalData.userInfo.avatarUrl,
            nickname: app.globalData.userInfo.nickName,
            title: app.globalData.title,
            point: app.globalData.point,
            historyScore: app.globalData.historyScore,
          }
        );
      }
    }.bind(this), 500)  

  },

  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    });
    app.login();
    if (app.globalData.backURL != '') {
      wx.navigateTo({
        url: app.globalData.backURL,
      })
    }
  },
  starTap: function () {
    if (app.globalData.userInfo == null) {
      wx.showToast({
        title: '请先登陆',
        image: '../../image/warn.png',
      })
      return;
    }
    wx.navigateTo({
      url: '../star/play',
    })
  },
  surenTap: function () {
    wx.navigateToMiniProgram({
      appId: 'wx0bc6c5ceddbae730',
    })
  },
  starRankTap: function () {
    if (app.globalData.userInfo == null) {
      wx.showToast({
        title: '请先登陆',
        image: '../../image/warn.png',
      })
      return;
    }
    wx.navigateTo({
      url: '../star/rank',
    })
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '有人@我,听说你对明星很了解，能找到ta们小时候的样子吗？',
	    imageUrl:'../../image/s.jpg',
      path: 'pages/index/index',
      success: function (res) {
        var shareTicket = (res.shareTickets && res.shareTickets[0]) || ''
        wx.getShareInfo({
          shareTicket: shareTicket,
          success: function (res) {    
			wx.showLoading({})
            common.request({ url: 'c=Game&a=descriptShare', data: { data: res.encryptedData, iv: res.iv }, 
            success: function (e) { 
                if(e.status==0 && e.point>0){
                  app.globalData.point = parseInt(e.point) + parseInt(app.globalData.point);
                  wx.showToast({
                    title: '获得积分'+e.point,
                    icon:'success'
                  })
                }
             },
			complete:function(){
				wx.hideLoading();
			},
			method: "post" });
          }
        })
      },
      fail: function (ret) {}
    }
  },
})



